<template>
  <v-data-table :headers="headers" :items="desserts" sort-by="calories" class="elevation-1">
    <template v-slot:top>
      <v-toolbar flat>
        <v-toolbar-title>传票解析规则</v-toolbar-title>
        <v-divider class="mx-4" inset vertical></v-divider>
        <v-spacer></v-spacer>
        <v-dialog v-model="dialog" max-width="800px">
          <template v-slot:activator="{ on }">
            <v-btn color="primary" dark class="mb-2" v-on="on">新增</v-btn>
          </template>
          <v-card>
            <v-card-title>
              <span class="headline">{{ formTitle }}</span>
            </v-card-title>

            <v-card-text>
              <v-container>
                <v-row>

                  
                  <v-col cols="12" sm="6" md="4">
                  <v-select :items="tranentnoItems" 
                  label="交易事件" 
                  v-model="editedItem.tranentno"
                  item-text="tranevntnm"
                  item-value="tranentno"
                  @change="selChange(editedItem)"
                  ></v-select>
                  </v-col>

                  <!-- <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.tranentno" label="交易事件码"></v-text-field>
                  </v-col>
                  <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.tranevntnm" label="交易事件名称"></v-text-field>
                  </v-col> -->
                  <v-col cols="12" sm="6" md="4">
                  <v-select :items="accprdnoItems" 
                  label="核算产品" 
                  v-model="editedItem.accprdno"
                  item-text="name"
                  item-value="attributes"
                  ></v-select>
                  </v-col>
                  <!-- <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.accfacennm" label="记账要素英文名称"></v-text-field>
                  </v-col> -->

                   <v-col cols="12" sm="6" md="4">
                  <v-select :items="accfacennmItems" 
                  label="记账要素" 
                  v-model="editedItem.accfaccode"
                  item-text="name"
                  item-value="attributes"
                  @change="selChange(editedItem)"
                  ></v-select>
                  </v-col>

                  
                   <!-- <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.accfacchnm" label="记账要素中文名称"></v-text-field>
                  </v-col> -->
               

                   <v-col cols="12" sm="6" md="4">
                  <v-select :items="dcflgItems" 
                  label="借贷方向" 
                  v-model="editedItem.dcflg"
                  item-text="name"
                  item-value="attributes"
                  ></v-select>
                  </v-col>

                   <v-col cols="12" sm="6" md="4">
                  <v-select :items="accentflgItems" 
                  label="记账方式" 
                  v-model="editedItem.accentflg"
                  item-text="name"
                  item-value="attributes"
                  ></v-select>
                  </v-col>
                     <v-col cols="12" sm="6" md="4">
                  <v-select :items="subsrctypItems" 
                  label="科目来源" 
                  v-model="editedItem.subsrctyp"
                  item-text="name"
                  item-value="attributes"
                  ></v-select>
                  </v-col>



                <v-col cols="12" sm="12" md="12">
                    <v-autocomplete
                      v-model="editedItem.busnoa"
                      :items="busnoaItems"
                      chips
                      clearable
                      hide-details
                      hide-selected
                      item-text="glSubjectInfo"
                      item-value="businessCode"
                      label="请选择业务代号"
                      solo
                    ></v-autocomplete>
                   
                  </v-col>
                  <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.seqno" label="账户序号"></v-text-field>
                  </v-col>


                  <v-col cols="12" sm="6" md="4">
                      <v-select 
                      :items="orgSourceItems" 
                      label="机构号来源" 
                      v-model="editedItem.brcsrcflg"
                    
                                  item-text="name"
                                  item-value="attributes"
                      ></v-select>
                  </v-col>
                  <v-col cols="12" sm="6" md="4">
                      <v-select 
                      :items="orgFlagItems" 
                      label="机构号标识" 
                      v-model="editedItem.brciden"
                      
                            item-text="name"
                            item-value="attributes"
                      ></v-select>
                  </v-col>
                   <v-col cols="12" sm="6" md="4">

                     <v-select 
                     :items="brcInfoItems" 
                     label="机构编号" 
                     v-model="editedItem.brc"
                      item-text="brcname"
                      item-value="brc"
                     ></v-select>
                  </v-col>

                  <!-- <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.brcsrcflg" label="机构号来源"></v-text-field>
                  </v-col>
                  <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.brciden" label="机构号标识"></v-text-field>
                  </v-col>
                   <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.brc" label="机构号"></v-text-field>
                  </v-col>
                    <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.amtexpform" label="金额表达式"></v-text-field>
                  </v-col> -->

                   
                  <v-col cols="12" sm="6" md="4">
                  <v-select :items="rcdstsItems" 
                  label="记录状态" 
                  v-model="editedItem.rcdsts"
                  item-text="name"
                  item-value="attributes"
                  ></v-select>
                  </v-col>
                </v-row>




                 <v-row>
                  <v-col cols="12" sm="12" md="12">
                    <v-text-field v-model="editedItem.amtexpform" label="金额表达式"></v-text-field>
                  </v-col>
                  <v-col cols="12" sm="12" md="12">
                      <v-card class="mx-auto"
                        max-width="600">
                        <v-card-title>
                          金额表达式拼接 
                          <v-spacer></v-spacer>
                          <v-btn color="blue darken-1"  @click="editedItem.amtexpform=''">清除</v-btn>
                        </v-card-title>
                        <v-card-text>

                          <v-chip-group
                           
                            column
                            
                          >
                          <v-chip v-for="item in elementItems2" :key="item.id"
                            filter
                            outlined
                            @click="addExp(item)"
                          >
                            {{item.name}}
                          </v-chip>
                          </v-chip-group>
                        
                        </v-card-text>
                      </v-card>
                  </v-col>
                </v-row>

              </v-container>
            </v-card-text>

            <v-card-actions>
              <v-spacer></v-spacer>
              <v-btn color="blue darken-1" text @click="close">取消</v-btn>
              <v-btn color="blue darken-1" text @click="save">保存</v-btn>
            </v-card-actions>
          </v-card>
        </v-dialog>
      </v-toolbar>
    </template>
    <template v-slot:item.actions="{ item }">
      <v-icon small class="mr-2" @click="editItem(item)">mdi-pencil</v-icon>
      <v-icon small @click="deleteItem(item)">mdi-delete</v-icon>
    </template>
    <template v-slot:no-data>
      <v-btn color="primary" @click="initialize">Reset</v-btn>
    </template>



       <template v-slot:item.tranevntnm="{ item }">
    
      <span v-for="s in tranentnoItems" key="s.id" v-if="s.tranentno==item.tranentno">{{s.tranevntnm}}</span>
    </template>

      <template v-slot:item.accfacchnm="{ item }">
    
      <span v-for="s in accfacennmItems" key="s.id" v-if="s.attributes==item.accfaccode">{{s.name}}</span>
    </template>


     <template v-slot:item.dcflg="{ item }">
    
      <span v-for="s in dcflgItems" key="s.id" v-if="s.attributes==item.dcflg">{{s.name}}</span>
    </template>

     <template v-slot:item.accentflg="{ item }">
    
      <span v-for="s in accentflgItems" key="s.id" v-if="s.attributes==item.accentflg">{{s.name}}</span>
    </template>

     <template v-slot:item.subsrctyp="{ item }">
    
      <span v-for="s in subsrctypItems" key="s.id" v-if="s.attributes==item.subsrctyp">{{s.name}}</span>
    </template>


     <template v-slot:item.brcsrcflg="{ item }">
    
      <span v-for="s in orgSourceItems" key="s.id" v-if="s.attributes==item.brcsrcflg">{{s.name}}</span>
    </template>

     <template v-slot:item.brciden="{ item }">
    
      <span v-for="s in orgFlagItems" key="s.id" v-if="s.attributes==item.brciden">{{s.name}}</span>
    </template>

    <template v-slot:item.rcdsts="{ item }">
    
      <span v-for="s in orgFlagItems" key="s.id" v-if="s.attributes==item.brciden">{{s.name}}</span>
    </template>

  </v-data-table>
</template>

<script>
export default {
  data: () => ({
    rcdstsItems: [],
    accentflgItems:[],
    subsrctypItems:[],
    dcflgItems:[],
    subsrctypItems:[],
    accfacennmItems:[],

    tranentnoItems:[],
     accprdnoItems:[],
     busnoaItems:[],
     brcInfoItems:[],
    orgSourceItems:[],
    orgFlagItems:[],
    dialog: false,
    headers: [
      {
        text: "交易事件码",
        align: "start",
        value: "tranentno"
      },
      { text: "交易事件名称", value: "tranevntnm" },
      { text: "核算产品号", value: "accprdno" },
      { text: "记账要素英文名称", value: "accfaccode" },
       { text: "记账要素中文名称", value: "accfacchnm" },
        { text: "借贷方向", value: "dcflg" },
           { text: "记账方式", value: "accentflg" },
           { text: "科目来源", value: "subsrctyp" },
           { text: "业务代码", value: "busnoa" },
           { text: "账户序号", value: "seqno" },
           { text: "机构号来源", value: "brcsrcflg" },
           { text: "机构号标识", value: "brciden" },
          { text: "机构号", value: "brc" },
        { text: "金额表达式", value: "amtexpform" },
      { text: "记录状态", value: "rcdsts" },
      { text: "操作", value: "actions", sortable: false }
    ],
    desserts: [],
    editedIndex: -1,
    editedItem: {
      tranentno: "",
      tranevntnm:"",
      accprdno: "",
      accfaccode:"",
      accfacchnm: "",
      dcflg:"",
      accentflg:"",
      subsrctyp:"",
      busnoa:"",
      seqno:"",
      brcsrcflg:"",
      brciden:"",
      brc:"",
      amtexpform:"",
      rcdsts:"1",
    },
    defaultItem: {
      tranentno: "",
      tranevntnm:"",
      accprdno: "",
      accfaccode:"",
      accfacchnm: "",
      dcflg:"",
      accentflg:"",
      subsrctyp:"",
      busnoa:"",
      seqno:"",
      brcsrcflg:"",
      brciden:"",
      brc:"",
      amtexpform:"",
      rcdsts:"1",
    },
      elementItems2:[
      {
        name:'(',
        attributes:'('
      },
      {
        name:')',
        attributes:')'
      },
      {
        name:'+',
        attributes:'+'
      },
      {
        name:'-',
        attributes:'-'
      },
      {
        name:'*',
        attributes:'*'
      },
      {
        name:'/',
        attributes:'/'
      },
      


      {
        name:'0',
        attributes:'0'
      },
      {
        name:'1',
        attributes:'1'
      },
      {
        name:'2',
        attributes:'2'
      },
      {
        name:'3',
        attributes:'3'
      },
      {
        name:'4',
        attributes:'4'
      },
      {
        name:'5',
        attributes:'5'
      },
      {
        name:'6',
        attributes:'6'
      },
      {
        name:'7',
        attributes:'7'
      },
      {
        name:'8',
        attributes:'8'
      },
      {
        name:'9',
        attributes:'9'
      },
      {
        name:'0.',
        attributes:'0.'
      },

    ],
  }),

  computed: {
    formTitle() {
      return this.editedIndex === -1 ? "新增" : "修改";
    }
  },

  watch: {
    dialog(val) {
      val || this.close();
    }
  },

  created() {
    this.initialize();
    this.getDict()
  },

  methods: {
    initialize() {
     this.axios
        .get("/acfb06/list")
        .then(response => {
          this.desserts = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
    },
    getDict(){

       this.axios
        .get("/dict/topName/记录状态")
        .then(response => {
          this.rcdstsItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
        this.axios
        .get("/dict/topName/记账方式")
        .then(response => {
          this.accentflgItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
        this.axios
        .get("/dict/topName/借贷方向")
        .then(response => {
          this.dcflgItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
         this.axios
        .get("/dict/topName/科目来源")
        .then(response => {
          this.subsrctypItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
        this.axios
        .get("/dict/topName/记账要素")
        .then(response => {
          this.accfacennmItems = response.data.data;
          this.accfacennmItems.forEach(ele=>{
            this.elementItems2.push(ele)
          })
        })
        .catch(response => {
          console.log(response.data.message);
        });

         this.axios
        .get("/acfb19/list")
        .then(response => {
          this.tranentnoItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
        this.axios
        .get("/dict/topName/核算产品")
        .then(response => {
          this.accprdnoItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
        this.axios
        .get("/subject/list")
        .then(response => {
          this.busnoaItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
        this.axios
        .get("/dict/topName/机构号来源")
        .then(response => {
          this.orgSourceItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });

         this.axios
        .get("/dict/topName/机构号标识")
        .then(response => {
          this.orgFlagItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
        this.axios
        .get("/brcInfo/list")
        .then(response => {
          this.brcInfoItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
       
    },
    editItem(item) {
      this.editedIndex = this.desserts.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog = true;
    },

    deleteItem(item) {
      const index = this.desserts.indexOf(item);
      confirm("确定要删除此条记录吗?") &&
        this.axios.delete('/acfb06/'+item.id)
          .then(res=>{
            this.desserts.splice(index, 1);
          })
        
    },

    close() {
      this.dialog = false;
      this.$nextTick(() => {
        this.editedItem = Object.assign({}, this.defaultItem);
        this.editedIndex = -1;
      });
    },

    save() {
       this.axios.post("/acfb06", this.$qs.stringify(this.editedItem)).then(res => {
          this.initialize();
          this.close();
      });
      
    },
     addExp(item) {
       this.editedItem.amtexpform+=item.attributes
      },
  
  }
};
</script>